// pages/myself/member/MyMember.js
let network = null;
let utils = null;
let api = null;
var Zan = require('../../../dist/index');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
const app = getApp();

Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    member: null,
    Expired: false,
    memberendtime: '',
    userInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    // 注册列表刷新事件
    WxNotificationCenter.addNotification("MemberRefreshNotification", this.refreshNotification, this);
    this.onPullDownRefresh();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    WxNotificationCenter.removeNotification("MemberRefreshNotification", this);
  },
  refreshNotification: function () {
    this.onPullDownRefresh();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    network._post_json(this, api.getUserInfo, { appId: app.globalData.appId, openId: app.globalData.openId }, getUserInfosuccess);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  openMember: function () {
    wx.navigateTo({
      url: '/pages/myself/member/Member',
    })
    // network._post_json(this, api.openMembercard, { id: this.data.member.id }, opensuccess);
  }
}))
var getUserInfosuccess = function (that, res) {
  if (res.data.data.memberendtime) {

    var NowTime = new Date().getTime();
    var total_micro = res.data.data.memberendtime * 1000 - NowTime;
    console.log("endTime" + res.data.data.memberendtime * 1000);
    console.log("NowTime" + NowTime);
    console.log("total_micro" + total_micro);
    if (total_micro <= 0) {
      that.setData({
        Expired: true
      })
    } else {
      that.setData({
        Expired: false
      })
      res.data.data.memberendtime = utils.formatDate02(new Date(res.data.data.memberendtime * 1000));
    }
  }
  app.globalData.userInfo = res.data.data;
  that.setData({
    typeId: res.data.data.typeId,
    memberendtime: res.data.data.memberendtime
  })
  network._post_json(that, api.getUserMemberType, { typeId: that.data.typeId }, memberTypeSuccess);
}
var memberTypeSuccess = function (that, res) {
  if (res.data.data) {
    that.setData({
      member: res.data.data
    })
  }
}
var opensuccess = function (that, res) {
  if (res.data.data) {
    if (res.data.data.isfree == 1) {
      wx.requestPayment({
        timeStamp: res.data.data.timeStamp,
        nonceStr: res.data.data.nonceStr,
        package: res.data.data.package,
        signType: res.data.data.signType,
        paySign: res.data.data.paySign,
        success: function (res) {
          that.showZanToast('开通成功！');

          //setTimeout(function () {
          WxNotificationCenter.postNotificationName('MyMemberRefreshNotification');
          wx.navigateTo({
            url: '/pages/myself/Myself',
          })
          //}, 2000);
          console.log(res)
        },
        fail: function (res) {
          console.log(res)
        },
      });
    } else {
      that.showZanToast('免费开通成功！');
      // setTimeout(function () {
      WxNotificationCenter.postNotificationName('MyMemberRefreshNotification');
      wx.navigateBack();
      //}, 2000);
    }
  }
}